/*
 * Copyright (C) 2015 UFreedom
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.ufreedom.floatingview;

import com.facebook.rebound.SpringSystem;
import com.ufreedom.floatingview.transition.FloatingTransition;
import com.ufreedom.floatingview.transition.YumFloating;
import com.ufreedom.floatingview.util.LogUtil;
import ohos.aafwk.ability.AbilitySlice;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.StackLayout;
import ohos.agp.utils.Rect;

/**
 * Floating
 *
 * @author UFreedom
 * @since 2021-04-23
 */
public class Floating {
    private StackLayout mFloatingDecorView;
    private SpringSystem mSpringSystem;

    /**
     * Floating
     *
     * @param ability
     * @param topComponent 传递顶层View
     * @throws NullPointerException Ability should not be null
     */
    public Floating(AbilitySlice ability, Component topComponent) {
        if (ability == null) {
            throw new NullPointerException("Ability should not be null");
        }
        mFloatingDecorView = new StackLayout(ability);

        mFloatingDecorView.setLayoutConfig(new StackLayout.LayoutConfig(StackLayout.LayoutConfig.MATCH_PARENT,
                StackLayout.LayoutConfig.MATCH_PARENT));

        ability.setUIContent(ResourceTable.Layout_floating_fecor_view);
        ComponentContainer rootView = (ComponentContainer) ability.findComponentById(ResourceTable.Id_root_view);
        rootView.addComponent(topComponent);
        rootView.addComponent(mFloatingDecorView);
        if (mSpringSystem == null) {
            mSpringSystem = SpringSystem.create();
        }
    }

    /**
     * startFloating
     *
     * @param floatingElement
     * @return Component
     */
    public Component startFloating(FloatingElement floatingElement) {

        // 原始图
        Component anchorView = floatingElement.anchorView;

        // 悬浮的视图
        Component targetView = floatingElement.targetView;
        if (targetView == null) {
            targetView = LayoutScatter.getInstance(anchorView.getContext()).parse(
                    floatingElement.targetViewLayoutResId, mFloatingDecorView, false);
        }

        Rect rect = anchorView.getComponentPosition();
        int[] location = anchorView.getLocationOnScreen();

        rect.left = location[0];
        rect.top = location[1];

        int widthMeasureSpec = Component.EstimateSpec.getSizeWithMode(targetView.getWidth(), Component.EstimateSpec.NOT_EXCEED);
        int heightMeasureSpec = Component.EstimateSpec.getSizeWithMode(targetView.getHeight(), Component.EstimateSpec.NOT_EXCEED);
        targetView.estimateSize(widthMeasureSpec, heightMeasureSpec);

        int topMargin = rect.top + ((anchorView.getHeight() - targetView.getHeight()) / 2) + floatingElement.offsetY;
        int leftMargin = rect.left + ((anchorView.getWidth() - targetView.getWidth()) / 2) + floatingElement.offsetX;

        StackLayout.LayoutConfig lp = new StackLayout.LayoutConfig(StackLayout.LayoutConfig.MATCH_CONTENT,
                StackLayout.LayoutConfig.MATCH_CONTENT);


        // 减去的状态栏高度
        int titleHeight = mFloatingDecorView.getLocationOnScreen()[1];
        lp.setMarginTop(topMargin <= 0 ? 0 : topMargin - titleHeight);
        lp.setMarginLeft(leftMargin);
        LogUtil.error("startFloating topMargin", topMargin + " leftMargin" + leftMargin);
        mFloatingDecorView.addComponent(targetView, lp);
        FloatingTransition floatingAnimator = floatingElement.floatingTransition;
        floatingAnimator.applyFloating(new YumFloating(targetView, mSpringSystem));
        return mFloatingDecorView;
    }
}

